package com.hexgon.plugins.rabbitmq;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.amqp.AbstractRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.boot.autoconfigure.amqp.RabbitRetryTemplateCustomizer;
import org.springframework.boot.context.properties.PropertyMapper;

import java.util.List;

/**
 * MultiRabbitListenerContainerFactoryConfigurer
 * @author: XuZhibin
 * @date: 2023/11/12 12:13
 * @version: v1.0
 * @attention: 改造 configure() 支持自定义 RabbitProperties 来源
 */
public class MultiRabbitListenerContainerFactoryConfigurer extends AbstractRabbitListenerContainerFactoryConfigurer<SimpleRabbitListenerContainerFactory> {
    public MultiRabbitListenerContainerFactoryConfigurer() {
    }

    public void configure(SimpleRabbitListenerContainerFactory factory, ConnectionFactory connectionFactory) {
        PropertyMapper map = PropertyMapper.get();
        RabbitProperties.SimpleContainer config = this.getRabbitProperties().getListener().getSimple();
        this.configure(factory, connectionFactory, config);
        config.getClass();
        map.from(config::getConcurrency).whenNonNull().to(factory::setConcurrentConsumers);
        config.getClass();
        map.from(config::getMaxConcurrency).whenNonNull().to(factory::setMaxConcurrentConsumers);
        config.getClass();
        map.from(config::getBatchSize).whenNonNull().to(factory::setBatchSize);
    }

    public void configure(RabbitProperties rabbitProperties, SimpleRabbitListenerContainerFactory factory, ConnectionFactory connectionFactory) {
        this.setRabbitProperties(rabbitProperties);
        this.configure(factory, connectionFactory);
    }

    @Override
    protected void setMessageConverter(MessageConverter messageConverter) {
        super.setMessageConverter(messageConverter);
    }

    @Override
    protected void setRetryTemplateCustomizers(List<RabbitRetryTemplateCustomizer> retryTemplateCustomizers) {
        super.setRetryTemplateCustomizers(retryTemplateCustomizers);
    }

    @Override
    protected void setRabbitProperties(RabbitProperties rabbitProperties) {
        super.setRabbitProperties(rabbitProperties);
    }
}
